package com.sanploy.manage.controller;

import com.sanploy.common.vo.BaseException;
import com.sanploy.manage.service.FreightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by zhu yingzhi on 2017/9/7.
 * @author yingzhi zhu
 * 运费相关请求路由
 */
@RestController
@RequestMapping("/freight")
public class FreightController
{
    @Autowired
    private FreightService freightService;

    /**
     * 插入运费模板
     * @param name 模板名
     * @param is_free 是否包邮
     * @param province 派送地址
     * @param city 派送地址
     * @param county 派送地址
     * @param detail 派送地址
     * @param position 经纬度
     * @param remote_json 远地址json  参考类 FreightModelRemote 示例 ： [{"transport":"EMS","price":10.0,"area_price":[10.0,10.0],"area":["西藏","新疆"]},{"transport":"EXPRESS","price":15.0,"area_price":[15.0,15.0],"area":["西藏","新疆"]}]
     * @param local_json 本地Json 参考类 FreightModelLocal 示例 {"transport":"LOCAL","price":10.0,"BaseKM":3,"KMPrice":5.0,"maxKM":10}
     * @return
     */
    @PostMapping("")
    public Object insertFreight(@RequestParam String name,@RequestParam boolean is_free,@RequestParam String province,
                             @RequestParam String city, @RequestParam String county,@RequestParam(defaultValue = "") String detail,
                                @RequestParam String position,@RequestParam(defaultValue = "") String remote_json,@RequestParam String local_json){

        return freightService.insertFreight(name, is_free, province,city,county,detail, position, remote_json, local_json);
    }

    /**
     * 根据Id 获取运费模板
     * @return
     */
    @GetMapping("/{id}")
    public Object getOne(@PathVariable Long id){
        return freightService.findOne(id);
    }


    /**
     * 修改运费模板接口
     * @param id
     * @param name
     * @param is_free
     * @param province
     * @param city
     * @param county
     * @param detail
     * @param position
     * @param remote_json
     * @param local_json
     * @return
     * @throws BaseException
     */
    @PostMapping("/{id}")
    public Object editFreight(@PathVariable Long id,@RequestParam String name,@RequestParam boolean is_free,@RequestParam String province,
                              @RequestParam String city, @RequestParam String county,@RequestParam(defaultValue = "") String detail,
                              @RequestParam String position,@RequestParam(defaultValue = "") String remote_json,@RequestParam String local_json) throws BaseException {
            return freightService.updateFreight(id, name, is_free, province, city, county, detail, position, remote_json, local_json);
    }

    /**
     * 查找所有运费模版
     * @return
     * @throws BaseException
     */
    @GetMapping("/all")
    public Object getAllFreight() throws BaseException {
        return  freightService.findAllFreight();
//        Gson gson = new Gson();
//
//        List<FreightModelRemote> list = new ArrayList<>();
//        FreightModelRemote freightModelRemote = new FreightModelRemote();
//        freightModelRemote.setArea(new String[]{"西藏","新疆"});
//        freightModelRemote.setArea_price(new double[]{10,10});
//        freightModelRemote.setPrice(10);
//        freightModelRemote.setTransport(Transport.EMS);
//        list.add(freightModelRemote);
//
//        FreightModelRemote freightModelRemote1 = new FreightModelRemote();
//        freightModelRemote1.setArea(new String[]{"西藏","新疆"});
//        freightModelRemote1.setArea_price(new double[]{15,15});
//        freightModelRemote1.setPrice(15);
//        freightModelRemote1.setTransport(Transport.EXPRESS);
//        list.add(freightModelRemote1);
//        System.out.println(gson.toJson(list));
//
//
//        FreightModelLocal freightModelLocal = new FreightModelLocal();
//        freightModelLocal.setPrice(10);
//        freightModelLocal.setBaseKM(3);
//        freightModelLocal.setKMPrice(5);
//        freightModelLocal.setMaxKM(10);
//        freightModelLocal.setTransport(Transport.LOCAL);
//        System.out.println(gson.toJson(freightModelLocal));

    }

    //
//    /**
//     * 根据Id 删除运费模板
//     * @param id
//     * @return
//     * @throws BaseException
//     */
//    @DeleteMapping("/{id}")
//    public Object deleteFreight(@PathVariable Long id) throws BaseException {
//        return freightService.deleteAllFreight(id);
//    }

    @GetMapping("asd")
    public String test(){
        return freightService.test();
    }




}
